﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using DSRSystemCore.Core;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace CompanySearchConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            //ImportCompaniesRaw("mongodb://localhost:27017/DSRRaw");
            //return;

            if (args == null || args.Length < 1)
            {
                Console.WriteLine("number of participants expected");
                return;
            }
            int pNo = 0;
            if (!int.TryParse(args[0], out pNo) || pNo < 1)
            {
                Console.WriteLine("number of participants expected > 0");
                return;
            }

            var mongoDb = GetMongoDatabase();
            TestSuite tests = new TestSuite(mongoDb, new MongoTestContext(mongoDb));
            tests.repeatNo = 10;
            tests.ExecuteTests(pNo);

            Console.Read();
        }



        private static void ImportCompaniesRaw(string connectionString)
        {
            var dbConnectionString = MongoUrl.Create(connectionString);

            var client = new MongoClient(dbConnectionString);
            var server = client.GetServer();
            var clientMongoDb = server.GetDatabase(dbConnectionString.DatabaseName);

            using (var context = new DSRSystemEntities())
            {
                var dbCompanyCollection = clientMongoDb.GetCollection<MongoEntities.Company>(typeof(MongoEntities.Company).Name);
                var dbTickers = clientMongoDb.GetCollection<MongoEntities.Ticker>(typeof(MongoEntities.Ticker).Name);
                var dbIsins = clientMongoDb.GetCollection<MongoEntities.Isin>(typeof(MongoEntities.Isin).Name);
                var dbSedols = clientMongoDb.GetCollection<MongoEntities.Sedol>(typeof(MongoEntities.Sedol).Name);


                dbCompanyCollection.CreateIndex(new IndexKeysBuilder().Ascending("Name"));
                dbTickers.CreateIndex(new IndexKeysBuilder().Ascending("Value"));
                dbIsins.CreateIndex(new IndexKeysBuilder().Ascending("Value"));
                dbSedols.CreateIndex(new IndexKeysBuilder().Ascending("Value"));


                foreach (var company in context.Companies)
                {
                    var dbCompany = new MongoEntities.Company();
                    //Mapper.Map(company, dbCompany);
                    dbCompany.Id = company.CompanyId;
                    dbCompany.Name = (company.Name ?? "").ToLower();

                    //foreach (var com in company.Name.Split('.', ' ', '-'))
                    //{
                    //    dbCompany.Words.Add(com.ToLower());
                    //}

                    foreach (var ticker in company.Tickers)
                    {
                        dbTickers.Save(new MongoEntities.Ticker { Id = ticker.TickerId, Value = ticker.Value.ToLower(), CompanyId = dbCompany.Id });
                    }


                    foreach (var item in company.ISINs)
                    {
                        dbIsins.Save(new MongoEntities.Isin { Id = item.ISINId, Value = item.Value.ToLower(), CompanyId = dbCompany.Id });
                    }


                    foreach (var item in company.Sedols)
                    {
                        dbSedols.Save(new MongoEntities.Sedol { Id = item.SedolId, Value = item.Value.ToLower(), CompanyId = dbCompany.Id });
                    }


                    dbCompanyCollection.Save(dbCompany);

                }
            }
        }
        /// <summary>
        ///
        /// </summary>


        private static MongoDatabase GetMongoDatabase()
        {

            string connectionString = "mongodb://localhost:27017/DSRRaw";
            var dbConnectionString = MongoUrl.Create(connectionString);

            var client = new MongoClient(dbConnectionString);
            var server = client.GetServer();
            var clientMongoDb = server.GetDatabase(dbConnectionString.DatabaseName);
            return clientMongoDb;
        }

    }


  
}
